// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    // 登录
        wx.login({
            success: res => {
              // 发送 res.code 到后台换取 openId, sessionKey, unionId
              // console.log(res.code);
              if (res.code) {
                wx.request({
                  // url: `/user/wxapp/authorize/code=${res.code}`,
                  url: 'https://api.it120.cc/linsong/user/wxapp/login',
                  data: {
                    code: res.code
                  },
                  success: function (res) {
                    // console.log(res);
                    // 存到本地存储
                    if (res.data.code == 0) {
                      wx.setStorageSync('uid', res.data.data.uid)
                      wx.setStorageSync('token', res.data.data.token)
                      wx.setStorageSync('openid', res.data.data.openid)
                    }
                  },
                  fail: function (err) {
                    // console.log(err);
                  }
                })
              } else {
                // console.log(error + '登录失败' + res.errMsg);
              }
            },
            fail: function (error) {
              // console.log(error + '错误');
            }
          })
       
  },
  globalData: {
    userInfo: null
  }
})
